﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Data;
namespace UIT_CV_Management
{
    public class ThongTinPhu
    {
        public String maThongTinPhu;

        public String nguyenQuan;
        public String danToc;
        public String diaChi;
        public String tonGiao;
        public String thanhPhanBanThan;
        public DateTime ngayVaoDang;
        public DateTime ngayVaoDoan;
        public String chucVuCaoNhat;
        public String noiCVCN;
        public DateTime thoiGianCVCN;
        public String tinhTrangSucKhoe;

        //database
        Database myDatabase = Database.shareDatabase;
        public ThongTinPhu(String _nguyenQuan, String _danToc, String _diaChi, String _tonGiao, String _thanhPhanBanThan, DateTime _ngayVaoDang, DateTime _ngayVaoDoan, String _chucVuCaoNhat, String _noiCVCN, DateTime _thoiGianCVCN, String _tinhTrangSucKhoe)
        {
            maThongTinPhu = this.getMaThongTinPhu();

            nguyenQuan = _nguyenQuan;
            danToc = _danToc;
            diaChi = _diaChi;
            tonGiao = _tonGiao;
            thanhPhanBanThan = _thanhPhanBanThan;
            ngayVaoDang = _ngayVaoDang;
            ngayVaoDoan = _ngayVaoDoan;
            chucVuCaoNhat = _chucVuCaoNhat;
            noiCVCN = _noiCVCN;
            thoiGianCVCN = _thoiGianCVCN;
            tinhTrangSucKhoe = _tinhTrangSucKhoe;
        }
        public ThongTinPhu(String _maThongTinPhu)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM ThongTinPhu WHERE MaThongTinPhu = '" + _maThongTinPhu + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;
                DataRow row = table.Rows[0];
                maThongTinPhu = table.Rows[0]["MaThongTinPhu"].ToString();
                nguyenQuan = table.Rows[0]["NguyenQuan"].ToString();
                danToc = table.Rows[0]["DanToc"].ToString();
                diaChi = table.Rows[0]["DiaChi"].ToString();
                tonGiao = table.Rows[0]["TonGiao"].ToString();
                thanhPhanBanThan = table.Rows[0]["ThanhPhanBanThan"].ToString();
                ngayVaoDoan = row.Field<DateTime>("NgayVaoDoan");
                ngayVaoDang = row.Field<DateTime>("NgayVaoDang");
                chucVuCaoNhat = table.Rows[0]["ChucVuCaoNhat"].ToString();
                noiCVCN = table.Rows[0]["NoiCVCN"].ToString();
                thoiGianCVCN = row.Field<DateTime>("ThoiGianCVCN");
                tinhTrangSucKhoe = table.Rows[0]["TinhTrangSucKhoe"].ToString();

            }
        }
        public String getMaThongTinPhu()
        {
            String x;

            // get from database
             x = Database.getID("ThongTinPhu", "MaThongTinPhu");

            return x;
        }
        public void save()
        {
                String sqlThongTinPhu = "INSERT INTO ThongTinPhu VALUES('" + maThongTinPhu + "','" + nguyenQuan + "','" + diaChi + "','" + danToc + "','" + tonGiao + "','" + thanhPhanBanThan + "','" + ngayVaoDoan.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + ngayVaoDang.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + chucVuCaoNhat + "','" + noiCVCN + "','" + thoiGianCVCN.Date.ToString("yyyy-MM-dd HH:mm:ss") + "','" + tinhTrangSucKhoe + "')";
                QueryResult result = myDatabase.Query(sqlThongTinPhu);

                if (result.ReturnCode == 409)
                    MessageBox.Show("Lỗi khi lưu dữ liệu Thông Tin Phụ vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        public void update()
        {
            bool isDupplicated = Database.isDupplicate("ThongTinPhu", "MaThongTinPhu", maThongTinPhu);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE ThongTinPhu SET NguyenQuan = '" + nguyenQuan + "', DiaChi = '" + diaChi + "', DanToc = '" + danToc + "',TonGiao = '" + tonGiao + "', ThanhPhanBanThan = '" + thanhPhanBanThan + "',NgayVaoDoan = '" + ngayVaoDoan.Date.ToString("yyyy-MM-dd HH:mm:ss") + "', NgayVaoDang = '" + ngayVaoDang.Date.ToString("yyyy-MM-dd HH:mm:ss") + "',ChucVuCaoNhat = '" + chucVuCaoNhat + "',NoiCVCN = '" + noiCVCN + "',ThoiGianCVCN = '" + thoiGianCVCN.Date.ToString("yyyy-MM-dd HH:mm:ss") + "',TinhTrangSucKhoe = '" + tinhTrangSucKhoe
                    + "' WHERE MaThongTinPhu = '" + maThongTinPhu + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    MessageBox.Show("Lỗi khi update liệu Thông Tin Phụ vào database", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
        public void delete()
        {
            String sqlDelete = "DELETE FROM ThongTinPhu WHERE MaThongTinPhu = '" + maThongTinPhu + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                MessageBox.Show("Lỗi xoá dữ liệu trong database.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
}
